package com.whao.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whao.entity.Result;
import com.whao.entity.User;
import com.whao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class DemoController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = {"/", "/index"})
    public String index() {
        return "welcome";
    }

    @RequestMapping(value = {"/toLogin"})
    public String login() {
        return "Login";
    }

    @RequestMapping(value = "/level1/{id}")
    public String level1(@PathVariable("id") int id) {
        return "views/level1/" + id;
    }

    @RequestMapping(value = "/level2/{id}")
    public String level2(@PathVariable("id") int id) {
        return "views/level2/" + id;
    }

    @RequestMapping(value = "/level3/{id}")
    public String level3(@PathVariable("id") int id) {
        return "views/level3/" + id;
    }

    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    @ResponseBody
    public Result doLogin(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User u = userMapper.selectOne(wrapper);
        Result result = new Result();
        if (u == null) {
            result.setCode(0);
            result.setMsg("该用户不存在");
            return result;
        }
        if (!password.equals(u.getPassword())) {
            result.setCode(0);
            result.setMsg("输入密码错误");
            return result;
        }
        result.setCode(1);
        result.setMsg("登录成功");
        return result;
    }

}
